using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace CodePlex.Resourceful
{
    public class Tracer
    {
        private readonly List<TracerListener> _listeners;

        public Tracer()
        {
            _listeners = new List<TracerListener>();
        }

        public List<TracerListener> Listeners
        {
            get
            {
                return _listeners;
            }
        }

        public bool HasListeners
        {
            get
            {
                return _listeners.Count > 0;
            }
        }

        public Tracer AddListener(string filename)
        {
            StreamWriter sw = File.CreateText(filename);
            sw.AutoFlush = true;
            return this.AddListener(sw);
        }

        public Tracer AddListener(TracerListener listener)
        {
            _listeners.Add(listener);
            return this;
        }
        public Tracer AddListener(TextWriter writer)
        {
            this.AddListener(TracerListener.From(writer));
            return this;
        }
        public Tracer AddListener(Tracer anotherTracer)
        {
            this.AddListener(TracerListener.From(anotherTracer));
            return this;
        }



        public void Write(string format, params object[] args)
        {
            this.Write(string.Format(format, args));
        }

        public void Write(string value)
        {
            foreach (TracerListener listener in this.Listeners)
            {
                listener.Write(value);
            }
        }
        public void WriteLine(string format, params object[] args)
        {
            this.WriteLine(string.Format(format, args));
        }

        public void WriteLine()
        {
            this.WriteLine("");
        }
        public void WriteLine(string value)
        {
            foreach (TracerListener listener in this.Listeners)
            {
                listener.WriteLine(value);
            }
        }

    }
}
